<%@page import="getools4me.BlogManager"%>
<%@page import="domain.BlogFile"%>
<%@page pageEncoding="UTF-8" %>
<%@page import="getools4me.UserManager"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%
	UserService us = UserServiceFactory.getUserService();
	User user = us.getCurrentUser();
	String thisUrl = request.getRequestURI();
	String loginURL = us.createLoginURL(thisUrl);
	String logoutURL = us.createLogoutURL(thisUrl);
	String blogID = request.getParameter("blogID");
	BlogFile file = null;
%>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/blog/css/common.css" />
<link rel="stylesheet" type="text/css" href="/blog/css/menu.css" />
<link rel="stylesheet" type="text/css" href="/blog/css/editor.css" />
<script type="text/javascript" src="/blog/js/common.js"></script>

<title>Edit</title>
</head>
<body>

<div class="menu">
	<ul>
		<li><a href="/blog/page/home.jsp">Home</a></li>
		<li><a href="/blog/page/all.jsp">All</a></li>
		<li><%if(UserManager.isLogin()){ %><a href="<%=UserManager.getLogoutURL(request)%>">Logout</a><%}else{ %><a href="<%=UserManager.getLoginURL(request)%>">Login</a><%} %></li>
		<li><%if(UserManager.isAdmin()){ %>
		<a href="/blog/page/new.jsp">New</a><%} %>
		</li>
	</ul>
</div>


<%if(user==null){ %> 
	<div class="center">
		<h1><a href="<%=loginURL%>">Please Login ...</a></h1>
	</div>
<%}else if(!user.getEmail().equals("jpgtama@gmail.com")) { %>
	<div class="center">
		<h1>Welcome, Mr. <%=user.getNickname() %> !</h1>
		<i>Sorry, you are not admin.(<a href="<%=logoutURL%>">Logout</a>)    </i>
	</div>
<%}else if(blogID==null || blogID.trim().length()==0 || !blogID.matches("\\d+")){ %>
	<div class="center">
		<h1><%=blogID %>&nbsp;&lt;-- Please offer correct blog id!</h1>
	</div>
<%}else if((file=BlogManager.getBlogFileById(Long.valueOf(blogID)))==null){ %>
	<div class="center">
		<h1><%=blogID %>&nbsp;&lt;-- Blog with this id not exist!</h1>
	</div>
<%}else{ %>

<div class="edit">

	<form action="/receive" method="post" enctype="multipart/form-data">
		<div>
			<label>Title</label><span class="required">*</span>
			<input type="text" id="title" onchange="setHiddenValue(this.id);" class="string" >
			<input type="hidden" id="title.target" class="unicode" name="title" value="<%=new String(file.getTitle())%>">
		</div>
		<div>
			<label>Body</label><span class="required">*</span>
			<textarea id="content" class="string" rows="30" cols="150" onchange="setHiddenValue(this.id);"></textarea>
			<textarea id="content.target" class="unicode" name="content" rows="30" cols="150" ><%=new String(file.getContent())%></textarea>
		</div>
		<div class="submit"><input type="submit" value="Submit"/></div>
		<input type="hidden" name="method" value="recevieBlog"/>
		<input type="hidden" name="posturl" value="/blog/page/home.jsp"/>
		<div>
			<label>Author</label><span class="required">*</span>
			<input id="author" class="string" type="text" size="150" value="<%=user.getNickname()%>" onchange="setHiddenValue(this.id);" >
			<input id="author.target" name="author" type="hidden" size="150" >
		</div>
		<div>
			<label>Web Site</label><span class="required">*</span>
			<input type="text" id="website" size="150" value="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%>" onchange="setHiddenValue(this.id);"  class="string">
			<input type="hidden" id="website.target" name="website" size="150" >
		</div>
		<div>
			<label>Email</label><span class="required">*</span>
			(<a href="<%=logoutURL%>">Logout</a>)
			<input type="text" id="email" size="150" value="<%=user==null?"":user.getEmail()%>" onchange="setHiddenValue(this.id);"  class="string">
			<input type="hidden" id="email.target" name="email" size="150" >
		</div>
	</form>

</div>

<%} %>

<div class="footer">

	<iframe class="footer" src="/footer.html" frameborder="0" width="100%"></iframe>
	
</div>

<script type="text/javascript">
	// show string out
	var comm = new Common();
	comm.getTargetID = function(id){
		return id.replace(".target","");
	}
	comm.convertUnicodeClassElementToTargetElement(document);
	
	// change the value of hidden input 
	function setHiddenValue(id){
		var id_hidden = id+".target";
		var text = document.getElementById(id).value;
		document.getElementById(id_hidden).value =  Common.getUnicodeFromString(text);
	}
	
	
</script>

</body>
</html>